package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.query.NewsQuery;
import cn.wolfcode.trip.base.service.INewsService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


@Controller
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private INewsService newsService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") NewsQuery qo) {
        PageInfo pageInfo = newsService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "/news/list";
    }

    @RequestMapping("/getContentById")
    @ResponseBody
    public NewsContent getContentById(Long id) {
        NewsContent content = newsService.getContentById(id);
        return content;
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult updateState(News news, MultipartFile file) {
        JsonResult result = new JsonResult();
        try {
            //如果上传了背景图片就设置背景图片
            if (file != null && file.getSize() > 0) {
                String coverUrl = UploadUtil.uploadQiniu(file);
                news.setCoverUrl(coverUrl);
            }
            newsService.saveOrUpdate(news);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

}
